<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="店铺">
          <j-dict-select-tag v-decorator="['storeId', validatorRules.storeId]" placeholder="请选择店铺" :triggerChange="true" dictCode="stm_store_info,name,store_id,status!=0"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="目标类型">
          <j-dict-select-tag  v-decorator="['targetType', validatorRules.targetType]" @change="handleTargetType" :type="'radioButton'" :triggerChange="true" dictCode="target_type"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="值类型">
          <j-dict-select-tag  v-decorator="['valueType', validatorRules.valueType]" @change="handleValueType" :type="'radioButton'" :triggerChange="true" dictCode="value_type"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :hidden="this.model.targetType!=='order'"
          label="整单优惠">
          <a-input-number v-decorator="[ 'discountValue', {} ]"/>
          <span class="ant-input-span" :hidden="this.model.valueType!=='amount'">  元</span>
          <span class="ant-input-span" :hidden="this.model.valueType!=='ratio'">  折</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :hidden="this.model.targetType!=='product'"
          label="最多可加">
          <a-input-number v-decorator="[ 'upperValue', {} ]"/>
          <span class="ant-input-span" :hidden="this.model.valueType!=='amount'">  元</span>
          <span class="ant-input-span" :hidden="this.model.valueType!=='ratio'">  折</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :hidden="this.model.targetType!=='product'"
          label="最多可减">
          <a-input-number v-decorator="[ 'lowerValue', {}]"/>
          <span class="ant-input-span" :hidden="this.model.valueType!=='amount'">  元</span>
          <span class="ant-input-span" :hidden="this.model.valueType!=='ratio'">  折</span>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'StmStoreDiscountModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        storeId: { rules: [{ required: true, message: '请选择店铺!' }] },
        targetType: { rules: [{ required: true, message: '请选择目标类型!' }] },
        valueType: { rules: [{ required: true, message: '请选择值类型!' }] },
        discountValue: { rules: [{ required: true, message: '请输入优惠值!' }] },
        upperValue: { rules: [{ required: true, message: '请输入改价上限!' }] },
        cancelFlag: { rules: [{ required: true, message: '请输入1:失效;0有效!' }] }
      },
      url: {
        add: '/stm/stmStoreDiscount/add',
        edit: '/stm/stmStoreDiscount/edit'
      }
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'storeId', 'targetType', 'valueType', 'discountValue', 'upperValue', 'lowerValue'))
        //时间格式化
      })

    },
    close() {
      this.$emit('close')
      this.visible = false
    },

    handleTargetType (e) {
      this.model.targetType = e;
      this.form.setFieldsValue({'discountValue':0});
      this.form.setFieldsValue({'upperValue':0});
      this.form.setFieldsValue({'lowerValue':0});
    },
    handleValueType (e) {
      this.model.valueType = e;
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }


  }
}
</script>

<style lang="less" scoped>

.ant-input-span {
  white-space:pre;
}
</style>